package com.lzy.chat.c_service;

import com.lzy.chat.a_entity.Message;
import com.lzy.chat.b_mapper.MessageMapper;
import com.lzy.chat.c_service.Exception.Exceptions;
import com.lzy.chat.c_service.Exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {


    private MessageMapper messageMapper;

    @Autowired
    public void setMessageMapper(MessageMapper messageMapper) {
        this.messageMapper = messageMapper;
    }

    @Override
    public Message storeMessage(Message message) {
        Integer res = messageMapper.insert(message);
        if (res != 1) throw new ServiceException(Exceptions.MessageInsertException);
        return message;
    }

    @Override
    public void readMessage(Integer mid) {
        Integer res = messageMapper.updateReadMessage(mid);
//        System.out.println(res);
        if (res != 1) throw new ServiceException(Exceptions.MessageUpdateException);
    }

    @Override
    public List<Message> getUnreadMessage(Integer uid) {
        return messageMapper.findUserUnreadMessage(uid);
    }


    @Override
    public List<Message> getHistoryMessage(Date date, Integer uid, Integer uid2) {
        return messageMapper.getHistoryMessage(date, uid, uid2);
    }

    @Override
    public List<Message> getUnreadGroupMessage(Integer gid, Date lastRead) {
        return messageMapper.getUserUnreadGroupMessage(gid, lastRead);
    }

    @Override
    public List<Message> getGroupHistoryMessage(Integer gid, Date date) {
        return messageMapper.getGroupHistoryMessage(gid, date);
    }
}
